<?php
namespace app\api\controller;

use app\common\controller\IndexBaseController;
use app\web\model\GoodsDiscountModel;
use app\web\model\GoodsGrabModel;
use app\web\model\GoodPintuansModel;
use app\web\model\GoodsScoreModel;
use app\web\model\AgentModel;
use app\web\model\AddressModel;
use app\web\model\OrderItemsModel;
use app\web\model\OrderModel;
use app\admin\model\Member as MemberModel;
use think\Db;
/**
 * 商品处理
 */
class StoreController extends IndexBaseController {

	protected $discount_model;
	protected $grab_model;
	protected $pintuan_model;
	protected $goodsScore_model;
	protected $agent_model;
	protected $member_model;
	protected $address_model;
	protected $orderItem_model;
	protected $order_model;
	protected function _initialize() {
		parent::_initialize();
		$this->discount_model = new GoodsDiscountModel();
		$this->grab_model = new GoodsGrabModel();
		$this->pintuan_model = new GoodPintuansModel();
		$this->goodsScore_model = new GoodsScoreModel();
		$this->agent_model = new AgentModel();
		$this->member_model = new MemberModel();
		$this->address_model = new AddressModel();
		$this->orderItem_model = new OrderItemsModel();
		$this->order_model = new OrderModel();
	}
	/**
	 * 获取店铺信息
	 */
	 public function shopIndex(){
	 	
	 }
	/**
	 * 获取首页信息
	 */
	 public function index(){
	 	$packs=$packs=db('line_package')->limit(4)->select();
		$zss=db('line_goods')->where("is_zs",1)->limit(4)->select();
		$theme=$this->getZts();
		$goods=db('line_goods')->select();
		return json(['pack'=>$packs,'zss'=>$zss,'zt'=>$theme]);
	 }
	 /**
	  * 获取主题
	  */
	  public function getZts(){
	  	if(cache("theme")){
	  		return cache("theme");
	  	}else{
	  		$theme=db('theme')->select();
			if($theme){
				cache("theme",$theme);
			}
			return $theme;
	  	}
	  }
	 /**
	  * 获取套餐
	  */
	 public function getPacks(){
	 	$packs=db('line_package')->limit(4)->select();
		return $this -> toJson(200, '',$packs); 
	 }
	 /**
	  * 获取赠送商品
	  */
	 public function getZss(){
	 	$goods=db('line_package')->where("is_zs",1)->limit(4)->select();
		return $this -> toJson(200, '',$goods); 
	 }
	/**
	 * POST
	 * 获取店铺上架产品
	 */
	 public function listGoods($page=1){
		$goodsType = request() -> param('goodsType');
		$store_id = request() -> param('store');
		$goodsType_model = $goodsType.'_model';
		if (!empty($goodsType) && !empty($store_id)) {
			
			$listGoods = $this ->$goodsType_model->where('agent_id',$store_id)->paginate(10);
			$listGoods['goodsType'] = $goodsType;
			if ($listGoods) {
				return $this -> toJson(200, '', $listGoods);
			} else {
				return $this -> toJson(405, '数据不存在');
			}
		} else {
			return $this -> toJson(402, '参数不能为空');
		}
	 }
	/**
	 * 
	 * 获取单个商品信息
	 */
	 public function getGoods(){
		$goodsType = request() -> param('goodsType');
		$goods_id = request() -> param('goods_id');
		if ($goods_id) {
			$goodsType_model = $goodsType.'_model';
			$cut_goods = $this ->$goodsType_model -> where('id', $goods_id) -> find();
			$cut_goods['agent_address'] = $this->agent_model->where('id',$cut_goods['agent_id'])->value('address');
			$cut_goods['content'] = html_entity_decode($cut_goods['content']);
			$cut_goods['goodsType'] = $goodsType;
			if ($cut_goods) {
				return $this -> toJson(200, '', $cut_goods);
			} else {
				return $this -> toJson(405, '数据不存在');
			}
		} else {
			return $this -> toJson(402, '参数不能为空');
		}
	 }
	 /**
	 * 用户购买
	 */
	 public function userBuy(){
	 	$goodsType = request() -> param('goodsType');
	 	$goods_id = request() -> param('goods_id');
		$user_id = request() -> param('user_id');
		$num = request() -> param('num');
		$goodsType_model =$goodsType."_model";
		$cut_goods = $this ->$goodsType_model -> where('id', $goods_id) -> find();
		if($cut_goods){
			$check['user_id'] = $user_id;
			$check['goods_id'] = $cut_goods['id'];
			$check['item_state'] = 0;
			$chenckOrderItem =$this ->orderItem_model -> where($check) -> find();
			
			if($chenckOrderItem){
				$newOrderItem['number'] = $num;
				$newOrderItem['add_time'] = now_time();
				$result =Db::name('mall_order_item')->where('id', $chenckOrderItem['id'])->update($newOrderItem);
				
				$orderItem_id =  $chenckOrderItem['id'];
				$orderItemInfo['orderItem_id'] = $orderItem_id;
				$orderItemInfo['goodsType'] = $goodsType;
			}else{
				$orderItem_model = new OrderItemsModel();
			
				$orderItem_model->goods_id   		=$cut_goods['id'];
				$orderItem_model->goods_name 		=$cut_goods['name'];
				$orderItem_model->sell_price 		=$cut_goods['current_price'];
				$orderItem_model->original_price 	=$cut_goods['original_price'];
				$orderItem_model->weight 			=$cut_goods['weight'];
				$orderItem_model->number 			=$num;
				$orderItem_model->thumb 			=$cut_goods['thumb'];	
				$orderItem_model->postage			=$cut_goods['postage'];	
				$orderItem_model->agent_id			=$cut_goods['agent_id'];
				$orderItem_model->add_time			=now_time();
				$orderItem_model->needScore			=$cut_goods['needScore'];
				$orderItem_model->needMoney			=$cut_goods['needMoney'];
				$orderItem_model->goods_lanmu		=$cut_goods['goods_lanmu'];
				$orderItem_model->user_id			=$user_id;
				//生成预支付订单
				$result = $orderItem_model->allowField(true)->save();
				
				$orderItem_id = $orderItem_model -> id;
				$orderItemInfo['orderItem_id'] = $orderItem_id;
				$orderItemInfo['goodsType'] = $goodsType;
			}
			if ($result) {
					return $this -> toJson(200, '', $orderItemInfo);
			} else {
					return $this -> toJson(405, '插入数据失败');
			}
		
		}else{
			return $this -> toJson(500, '', "订单错误");
		}
	 }
	/**
	 * 获取订单详情
	 */
	public function getOrderItem(){
		$goodsType = request() -> param('goodsType');
		$orderItem_id = request() -> param('orderItem_id');
		$user_id = request() -> param('user_id');
		$goodsType_model = $goodsType.'_model';
		$userInfor = $this ->member_model -> where('id', $user_id) -> find();
		$orderItemInfor =$this->orderItem_model->where('id',$orderItem_id)->find();
		$addressList = $this ->address_model->where('uid',$user_id)->select();
		if($userInfor && $orderItemInfor){
			$orderItemInfor['userName'] = $userInfor['real_name'];
			$orderItemInfor['mobile'] = $userInfor['mobile'];
			$orderItemInfor['addressList'] = $addressList;
			return $this -> toJson(200, '', $orderItemInfor);
		}else{
			return $this -> toJson(405, '数据异常');
		}
		
		
		
	
	}


	/*
	 *获取用户地址信息
	 */
	public function getAddress(){
		
		$address_id = request() -> param('address_id');
		$address = Db::name('mall_address')->where('id', $address_id)->find();
		if($address){
			return $this -> toJson(200, '', $address);
		}else{
			return $this -> toJson(405, '数据异常');
		}

	}
	
	/**
	 * 提交订单
	 * 单商品订单
	 */
	public function AddOrder(){
		$address_id = request() -> param('address_id');
		$orderItem_id = request() -> param('orderItem_id');
		$order_mes = request() -> param('order_mes');
		$payType = request() -> param('payType');
		
		$addressInfo = Db::name('mall_address')->where('id', $address_id)->find();
		$orderItemInfo = Db::name('mall_order_item')->where('id', $orderItem_id)->find();
		
		if($addressInfo && $orderItemInfo){
			
			$new_order =new OrderModel();
			
			//多商品
			$orderItem_ids = json_encode(array($orderItem_id),JSON_UNESCAPED_UNICODE);
			
			//总价
			$total_price = $orderItemInfo['sell_price']*$orderItemInfo['number'];
			
			$new_order->agent_id 			=$orderItemInfo['agent_id'];
			$new_order->user_id				=$orderItemInfo['user_id'];
			$new_order->orderItem_ids		=$orderItem_ids;		//应对多个详情
			$new_order->order_type			=$orderItemInfo['goods_lanmu'];
			$new_order->order_state			=$orderItemInfo['item_state'];
			$new_order->total_price			=$total_price;
			$new_order->postage				=$orderItemInfo['postage'];
			$new_order->pay_type			=$payType;
			$new_order->pay_mode			=0;			//付款模式有什么用？
			$new_order->pay_price			=$total_price;
			$new_order->order_time			=now_time();
			$new_order->receiver			=$addressInfo['realname'];
			$new_order->receiver_mobile		=$addressInfo['mobile'];
			$new_order->province			=$addressInfo['province'];
			$new_order->city				=$addressInfo['city'];
			$new_order->district			=$addressInfo['district'];
			$new_order->address				=$addressInfo['address'];
			$new_order->message				=$order_mes;
			$new_order->user_id				=$orderItemInfo['user_id'];
			
			//生成预支付订单
			$result = $new_order->allowField(true)->save();
			
			if ($result) {
					return $this -> toJson(200, '', $new_order);
			} else {
					return $this -> toJson(405, '插入数据失败');
			}
		}else{
			return $this -> toJson(500, '信息异常');
		}
		
		
		
		
		
	}
}
